package com.sanjiahantang.ythshopping.adapter.home;



import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.sanjiahantang.ythshopping.R;
import com.sanjiahantang.ythshopping.activity.MainActivity;
import com.sanjiahantang.ythshopping.model.MainNearByPageData;

import java.util.List;

/**
 * Created by admin on 2017/8/23.
 */

public class NearbyAdapter extends RecyclerView.Adapter <RecyclerView.ViewHolder>{

    Context context;
    List<Integer> typelist;
    LayoutInflater inflater;
    MainNearByPageData data;
    MainActivity activity;

    private final int TYPE_MENU=1;
    private final int TYPE_RECOMMEND=2;
    private final int TYPE_NEARBY=3;
    private final int TYPE_NEARBY_ITEM = 4;

    private int nearCounter;

    public NearbyAdapter(Context context,List<Integer> typelist)
    {
        this.context=context;
        this.typelist= typelist;
        inflater=LayoutInflater.from(context);


    }
    public NearbyAdapter(Context context, List<Integer> typelist,MainNearByPageData data, MainActivity activity) {
        this(context,typelist);
        this.data = data;
        this.activity = activity;
        if (data != null && data.getShop() != null){
            for (int i = 0,max = data.getShop().size();i<max;i++){
                this.typelist.add(TYPE_NEARBY_ITEM);
            }
        }
        nearCounter = 0;
    }

    @Override
    public int getItemViewType(int position) {
        return typelist.get(position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType)
        {
            case TYPE_MENU:
                return new ClassifyHolder(inflater.inflate(R.layout.rv_item_classify, null),
                        context, data.getMenu(), activity,GvSubjectAdapter.TYPE_NEAR);
            case TYPE_RECOMMEND:
                return new AllianceHolder(context,inflater.inflate(R.layout.rv_item_classify, null),data.getShop(),AllianceHolder.TYPE_NEAR_BY);
            case TYPE_NEARBY:
                return new NearByHolder(context,inflater.inflate(R.layout.rv_item_comprehesiva, null),data.getNear_shops());
            case TYPE_NEARBY_ITEM:
                if (data != null && data.getNear_shops() != null && nearCounter < data.getNear_shops().size()){
                    nearCounter++;
                }
                return new NearByHolder.ItemHolder(inflater.inflate(R.layout.item_neaby_long,null),data.getNear_shops().get(nearCounter > 0 ? (nearCounter - 1) : 0),context);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyBaseViewHolder) {
            ((MyBaseViewHolder) holder).bindData();
        }
    }

    @Override
    public int getItemCount() {
        return typelist.size();
    }
}
